package com.umakr.ax.app.job;

import com.umakr.ax.common.model.PluginCoupon;
import com.umakr.ax.common.model.PluginCouponResult;
import com.umakr.ax.common.service.PluginCouponResultService;
import com.umakr.ax.common.service.PluginCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * @author sc
 * @date 2018/1/5
 */
@Service
@EnableScheduling
public class PluginCouponExpireJob {

    @Autowired
    PluginCouponResultService resultService;

    @Autowired
    PluginCouponService pluginCouponService;

    /**
     * 过期优惠券查询更新
     */

    /**
     * //@Scheduled(cron = "0/10 * * ? * *") 每隔五秒执行一次
     * //@Scheduled(cron = "0 0/1 * * * ?")每隔1分钟
     * //@Scheduled(cron = "0 0 0 * * ?")每天凌晨0点触发
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void work(){

        List<PluginCouponResult> pcrList = resultService.selectList(null);

        pcrList.forEach((PluginCouponResult pcr) -> {
            PluginCoupon pc = pluginCouponService.queryById(pcr.getCouponid());
            if(pc != null && pc.getRpcEndtime().getTime() < pcr.getGetTime().getTime()){
                    pcr.setUseStatus(2);
                    resultService.update(pcr);
            }
        });

    }

}
